#include "UserManager.h"

ReturnStatus UserManager::registerUser(const string& username, const string& password,const Role role){
    
    //DatabaseManager Database("homeS.db");
    if(Database.connect()==ReturnStatus::SUCCESS){
        
        string query = "INSERT INTO users (username, password,role) VALUES ('"+username+"', '"+password+"','"+to_string(static_cast<bool>(role))+"')";
        if(Database.executeQuery(query)==ReturnStatus::SUCCESS){
            cout<<REGSUCCESS<<endl;
            return SUCCESS;
        }
    }
    cout<<REGFAILURE<<endl;
    return FAILURE;
}
User* UserManager::loginUser(const string& username, const string& password){

   // DatabaseManager Database("homeS.db");
    if(Database.connect()==ReturnStatus::SUCCESS){
        string query = "SELECT * FROM users WHERE username = '"+username+"' AND password = '"+password+"'";
        vector<string> results = Database.fetchResults(query,TABLEUSER);
        if(results.size()>0){
            User* user = new User(results[1],results[2],stoi(results[0]),stoi(results[3])==1?ROLEADMIN:ROLEUSER);
            
            cout<<LOGSUCCESS<<endl;
            return user;
        }
    }
    cout<<LOGFAILURE<<endl;
    return nullptr;
}
void UserManager::logoutUser(int userId){

    

}
// UserManager::UserManager(const string& dbName){
//     this->dbName=dbName;
// }